#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/time.h>
 
#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "user_define.h"
#include "node_define.h"
#include "data_define.h"

#include "event_node.h"
// add para lib_include

char Buf[DIGEST_SIZE*4];

int event_node_init(void * sub_proc, void * para)
{
	int ret;
	// add yorself's module init func here
	return 0;
}

int event_node_start(void * sub_proc, void * para)
{
	int ret;
	void * recv_msg;
	int type;
	int subtype;
	// add yorself's module exec func here
	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
			message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(EVENT_DEFINE))&&(subtype==SUBTYPE(EVENT_DEFINE,EVENT_CONTROL)))
		{
			ret=proc_event_node_control(sub_proc,recv_msg);
		}
	}
	return 0;
}

int proc_event_node_control(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(NODE_DEFINE,CONFIG) * node_config;
	RECORD(NODE_DEFINE,CONFIG) * input_config;
	RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	void * new_msg;

	ret=message_get_record(recv_msg,&event_control,0);
	if(ret<0)
		return ret;
    // get name from login_input's name
    // the input info is user_name:role, while role can be SYSADMIN, USER,etc

    switch(event_control->command)
    {
        case CTRL_START:
            ret = proc_event_node_start(sub_proc,recv_msg);
            break;
        case CTRL_HALT:
            ret = proc_event_node_halt(sub_proc,recv_msg);
            break;
        case CTRL_CONFIG:
            ret = proc_event_node_config(sub_proc,recv_msg);
            break;
        default:
            return -EINVAL;
    }
    return ret;
}

int proc_event_node_start(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(NODE_DEFINE,CONFIG) * node_config;

	node_config = proc_share_data_getpointer();
    node_config ->node_state = NODE_WORKING;
    proc_share_data_setpointer(node_config);

	ret=message_add_expand_data(recv_msg,TYPE_PAIR(NODE_DEFINE,CONFIG),node_config);
	if(ret<0)
		return ret;
	ret=ex_module_sendmsg(sub_proc,recv_msg);
	return ret;
}

int proc_event_node_halt(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(NODE_DEFINE,CONFIG) * node_config;

	node_config = proc_share_data_getpointer();
    node_config ->node_state = NODE_HALT;
    proc_share_data_setpointer(node_config);

	ret=message_add_expand_data(recv_msg,TYPE_PAIR(NODE_DEFINE,CONFIG),node_config);
	if(ret<0)
		return ret;
	ret=ex_module_sendmsg(sub_proc,recv_msg);
	return ret;
}

int proc_event_node_config(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(NODE_DEFINE,CONFIG) * input_config;
	RECORD(NODE_DEFINE,CONFIG) * node_config;
	RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
    MSG_EXPAND * msg_expand;
	void * new_msg;

    ret=message_remove_expand(recv_msg,TYPE_PAIR(NODE_DEFINE,CONFIG),&msg_expand);
    if(ret<0)
           return ret;

    input_config=msg_expand->expand;       

	node_config = proc_share_data_getpointer();

   // fill the node control struct  
   if(input_config->interval != 0)
        node_config->interval=input_config->interval;
   if(input_config->alarm_upper != 0)
        node_config->alarm_upper = input_config->alarm_upper;
   if(input_config->alarm_lower != 0)
        node_config->alarm_lower = input_config->alarm_lower;
    
    proc_share_data_setpointer(node_config);


	ret=message_add_expand_data(recv_msg,TYPE_PAIR(NODE_DEFINE,CONFIG),node_config);
	if(ret<0)
		return ret;
	ret=ex_module_sendmsg(sub_proc,recv_msg);
	return ret;
}
